package com.example.locationtracking.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.locationtracking.R;
import com.example.locationtracking.service.Service;
import com.example.locationtracking.service.impl.ServiceFactory;
import com.example.locationtracking.utils.Const;
import com.example.locationtracking.utils.LTUtils;

public class LoginActivity extends BaseActivity{

	private Button login_button;
	private Button cancel_button;
	private Button register_button;
	private EditText userName;
	private EditText password;
	public void onCreate(Bundle saveInstance){
		super.onCreate(saveInstance);
		
		loadData();
		setControls();
		
	}
	@Override
	protected void initUI() {
		// TODO Auto-generated method stub
		setContentView(R.layout.login_activity);
		
	}
	private void loadData(){
		login_button = (Button) findViewById(R.id.bt_login_ok);
		cancel_button = (Button) findViewById(R.id.bt_cancel_login);
		register_button = (Button) findViewById(R.id.bt_register_login);
		userName = (EditText) findViewById(R.id.txt_user_name_login);
		password = (EditText) findViewById(R.id.txt_password_login);
	}
	
	private void setControls(){
		
		login_button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (userName.getText().toString().isEmpty()||password.getText().toString().isEmpty()){
					Toast.makeText(getApplicationContext(), "User Name or Password isn't empty!", Toast.LENGTH_SHORT).show();
					return;
				} else {
					Thread thread = new Thread(new Runnable() {

						@Override
						public void run() {
							Message msg = handler.obtainMessage();
							Service service = ServiceFactory.getInstance()
									.getService();
							try {
								boolean isLogin = service.login(userName.getText().toString(), password.getText().toString());
								msg.obj=isLogin;
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							handler.sendMessage(msg);
						}
					});
					showProgressDlg();
					thread.start();
				}
			}
		});
		register_button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
				startActivity(intent);
				LoginActivity.this.finish();
			}
		});
		cancel_button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				LoginActivity.this.finish();
			}
		});
	}
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler(){
		public void handleMessage (Message msg){
			dismissProgressDlg();
			if (msg.obj!=null){
			boolean isLogin = (Boolean) msg.obj;
			if (isLogin){
				LTUtils.saveValue(getApplicationContext(), Const.USER_NAME_KEY, userName.getText().toString());
				Intent intent = new Intent(getApplicationContext(), MapActivity.class);
				intent.putExtra(Const.USER_NAME_KEY, userName.getText().toString());
				startActivity(intent);
				LoginActivity.this.finish();
			} else {
				Toast.makeText(getApplicationContext(), "User Name or Password is incorrect!", Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
				startActivity(intent);
				LoginActivity.this.finish();
			}
			/*Log.e("register response",msg.obj+"");*/
		}
			else {
				showNetworkErrorDlg();
			}
		}
	};
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	   return false;
	}
}
